﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperJoe.Framework.UnitOfWorkSql;
using WWX.Admin.IDAL;
using WWX.Dto;
using WWX.Dto.Search;
using WWX.Model;

namespace WWX.Admin.DAL
{
    public class OrderDAL : BaseDAL, IOrderDAL
    {
        public OrderDAL(IUnitOfWork uow) : base(uow)
        {
        }

        public void Add(Order order)
        {
            var sql = "insert into orders(id,no,memberid,productinfo,purchaseprice,saleprice,logisticsinfo,status,remark,date,createdtime,createdby,updatedtime,updatedby,isdeducted,remainpaymentmoney)values(?id,?no,?memberid,?productinfo,?purchaseprice,?saleprice,?logisticsinfo,?status,?remark,?date,?createdtime,?createdby,?updatedtime,?updatedby,?isdeducted,?remainpaymentmoney)";
            var parameters = new Dictionary<string, object>();
            parameters.Add("id", order.Id);
            parameters.Add("no", order.No);
            parameters.Add("memberid", order.MemberId);
            parameters.Add("productinfo", order.ProductInfo);
            parameters.Add("purchaseprice", order.PurchasePrice);
            parameters.Add("saleprice", order.SalePrice);
            parameters.Add("logisticsinfo", order.LogisticsInfo);
            parameters.Add("status", order.Status);
            parameters.Add("remark", order.Remark);
            parameters.Add("date", order.Date);
            parameters.Add("createdtime", order.CreatedTime);
            parameters.Add("createdby", order.CreatedBy);
            parameters.Add("updatedtime", order.UpdatedTime);
            parameters.Add("updatedby", order.UpdatedBy);
            parameters.Add("isdeducted", order.IsDeducted);
            parameters.Add("remainpaymentmoney", order.RemainPaymentMoney);
            this.Execute(sql, parameters);
        }

        public List<OrderAdminDto> GetPageList(OrderAdminSearch search, int pageNo, int pageSize, out int total)
        {
            List<OrderAdminDto> retList = null;
            Dictionary<string, object> parameters = null;
            var whereSql = GenerateWhereSql(search, out parameters);

            string totalSql = "select count(1) from orders o, members m where o.memberid=m.id " + whereSql;
            total = this.GetCount(totalSql, parameters);
            if (total != 0)
            {
                var pagingSql = "select o.*,m.phone memberphone, m.account memberaccount, m.truename membertruename, m.nickname membernickname,m.phone memberphone from orders o, members m where o.memberid=m.id " + whereSql + " order by date desc limit " + (pageNo - 1) * pageSize + "," + pageSize;

                retList = this.GetModels<OrderAdminDto>(pagingSql, parameters);

            }

            return retList ?? new List<OrderAdminDto>();
        }

        private string GenerateWhereSql(OrderAdminSearch search, out Dictionary<string, object> parameters)
        {
            parameters = this.CreateParameters();
            string sql = "";

            if (search.EndDate.HasValue)
            {
                sql += " and date<'" + search.EndDate.Value.AddDays(1).Date.ToString() + "'";
            }

            if (search.StartDate.HasValue)
            {
                sql += " and date>='" + search.StartDate.Value.Date.ToString() + "'";
            }

            if (search.ProductInfo.IsNotNullOrWhiteSpace())
            {
                sql += " and o.productinfo like ?productinfo";
                parameters.Add("productinfo", "%" + search.ProductInfo.Trim() + "%");
            }


            if (search.LogisticsInfo.IsNotNullOrWhiteSpace())
            {
                sql += " and o.logisticsinfo like ?logisticsinfo";
                parameters.Add("logisticsinfo", "%" + search.LogisticsInfo.Trim() + "%");
            }

            if (search.MemberAccount.IsNotNullOrWhiteSpace())
            {
                sql += " and m.account like ?account";
                parameters.Add("account", "%" + search.MemberAccount.Trim() + "%");
            }

            if (search.MemberPhone.IsNotNullOrWhiteSpace())
            {
                sql += " and m.phone like ?phone";
                parameters.Add("phone", "%" + search.MemberPhone.Trim() + "%");
            }

            if (search.MemberTrueName.IsNotNullOrWhiteSpace())
            {
                sql += " and m.truename like ?account";
                parameters.Add("account", "%" + search.MemberTrueName.Trim() + "%");
            }

            if (search.MemberNickName.IsNotNullOrWhiteSpace())
            {
                sql += " and m.nickname like ?nickname";
                parameters.Add("nickname", "%" + search.MemberNickName.Trim() + "%");
            }

            if (search.No.IsNotNullOrWhiteSpace())
            {
                sql += " and o.no like ?no";
                parameters.Add("no", "%" + search.No.Trim() + "%");
            }

            if (search.Remark.IsNotNullOrWhiteSpace())
            {
                sql += " and o.remark like ?remark";
                parameters.Add("remark", "%" + search.Remark.Trim() + "%");
            }

            if (search.IsDeducted.HasValue)
            {
                sql += " and o.isdeducted=?isdeducted";
                parameters.Add("isdeducted", search.IsDeducted.Value);
            }

            return sql;
        }

        public void Update(Order order)
        {
            var sql = "update orders set productinfo=?productinfo,logisticsinfo=?logisticsinfo,remark=?remark,saleprice=?saleprice, purchaseprice=?puchaseprice,updatedtime=?updatedtime,updatedby=?updatedby, date=?date,status=?status,isdeducted=?isdeducted, remainpaymentmoney=?remainpaymentmoney where id=?id";
            var parameters = new Dictionary<string, object>();
            parameters.Add("id", order.Id);
            parameters.Add("productinfo", order.ProductInfo);
            parameters.Add("logisticsinfo", order.LogisticsInfo);
            parameters.Add("remark", order.Remark);
            parameters.Add("puchaseprice", order.PurchasePrice);
            parameters.Add("updatedtime", order.UpdatedTime);
            parameters.Add("updatedby", order.UpdatedBy);
            parameters.Add("saleprice", order.SalePrice);
            parameters.Add("date", order.Date);
            parameters.Add("status", order.Status);
            parameters.Add("isdeducted", order.IsDeducted);
            parameters.Add("remainpaymentmoney", order.RemainPaymentMoney);
            this.Execute(sql, parameters);
        }

        public Order Get(string id)
        {
            var sql = "select * from orders where id=?id";
            return this.GetModel<Order>(sql, new Dictionary<string, object>() { { "id", id } });
        }
    }
}
